<template>
    <el-submenu
            :index="menu.menuId + ''"
            :popper-class="'site-sidebar--' + sidebarLayoutSkin + '-popper'"
            v-if="menu.list && menu.list.length >= 1">
        <template slot="title">
            <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
            <span>{{ menu.name }}</span>
        </template>
        <sub-menu
                :dynamicMenuRoutes="dynamicMenuRoutes"
                :key="item.menuId"
                :menu="item"
                v-for="item in menu.list">
        </sub-menu>
    </el-submenu>
    <el-menu-item :index="menu.menuId + ''" @click="gotoRouteHandle(menu)" v-else>
        <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
        <span>{{ menu.name }}</span>
    </el-menu-item>
</template>

<script>
    import SubMenu from './main-sidebar-sub-menu'

    export default {
        name: 'sub-menu',
        props: {
            menu: {
                type: Object,
                required: true
            },
            dynamicMenuRoutes: {
                type: Array,
                required: true
            }
        },
        components: {
            SubMenu
        },
        computed: {
            sidebarLayoutSkin: {
                get() {
                    return this.$store.state.common.sidebarLayoutSkin
                }
            }
        },
        methods: {
            // 通过menuId与动态(菜单)路由进行匹配跳转至指定路由
            gotoRouteHandle(menu) {
                var route = this.dynamicMenuRoutes.filter(item => item.meta.menuId === menu.menuId)
                if (route.length >= 1) {
                    this.$router.push({name: route[0].name})
                }
            }
        }
    }
</script>
